package com.ng.finance.api.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.ng.finance.api.entity.Wechat;
import com.ng.finance.api.entity.WechatUser;
import com.ng.finance.api.mapper.WechatUserMapper;
import com.ng.finance.api.service.UserPayService;
import com.ng.finance.api.service.WechatUserService;
import com.ng.finance.api.vo.FinanceIndexListVo;
import com.ng.finance.api.vo.UserVo;
import com.ng.finance.common.config.WeChatConfig;
import com.ng.finance.common.constant.Api;
import com.ng.finance.common.constant.ResultCode;
import com.ng.finance.common.pojo.Result;
import com.ng.finance.common.util.ApiTokenUtils;
import com.ng.finance.common.util.HttpClientUtil;
import lombok.extern.java.Log;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 83673
 * @since 2018-09-13
 */
@Service
@Log
public class WechatUserServiceImpl extends ServiceImpl<WechatUserMapper, WechatUser> implements WechatUserService {
    @Resource
    WeChatConfig weChatConfig;

    @Resource
    UserPayService userPayService;

    @Resource
    private ApiTokenUtils apiTokenUtils;

    @Override
    public Result getCode(String code) {
        String wechatResult = HttpClientUtil.get(Api.codeTokenUrl + "?appid=" + weChatConfig.getAppId() + "&secret=" + weChatConfig.getSecret() + "&js_code=" + code + "&grant_type=authorization_code");
        Gson gson = new Gson();
        Wechat wechat = gson.fromJson(wechatResult, Wechat.class);
        WechatUser wechatUser;
        if (StringUtils.isNotBlank(wechat.getUnionid())) {
            // 获取用户信息
            wechatUser = baseMapper.selectOne(new WechatUser(wechat.getUnionid()));
            if (ObjectUtils.isEmpty(wechatUser)) {
                //用戶未綁定
                wechat.setBdTag(2);
                return Result.build(ResultCode.SUCCESS, wechat);
            }

        } else {
            return Result.build(ResultCode.FORBIDDEN, wechat);
        }
        wechat.setBdTag(1);
        wechat.setRole(wechatUser.getRole());
        return Result.build(ResultCode.SUCCESS, wechat);
    }

    /**
     * 绑定
     *
     * @param wechatUser
     * @return
     */
    @Override
    public Result binDingWechatUser(WechatUser wechatUser) {
        String unionId = apiTokenUtils.getUnionId();
        WechatUser wechatUser1 = baseMapper.getWechatUser(unionId);
        if (!ObjectUtils.isEmpty(wechatUser1)) {
            return Result.build(ResultCode.FAILED, "用戶已绑定");
        }
        wechatUser.setUnionid(unionId);
        Integer integer = baseMapper.insert(wechatUser);
        return Result.build(integer > 0 ? ResultCode.SUCCESS : ResultCode.FAILED, integer > 0 ? "绑定成功" : "绑定失败");
    }

    @Override
    public Result getFinanceIndex() {
        //查询昨日消费
        Double yesterdayPay = userPayService.selectYesterdayPay();
        //查询总消费
        Double payTotal = userPayService.getTotalPay();

        //查询今日消费
        Double payTodayTotal = userPayService.getTodayTotalPay();

        return Result.build(ResultCode.SUCCESS, new FinanceIndexListVo(yesterdayPay, payTotal, payTodayTotal));

    }


    /**
     * 获取首页  流水账
     *
     * @return
     */
    @Override
    public Result getFinanceIndexList(String payTime, Page page) {
        //查询 本月消费
        Double payMonth = userPayService.getMonthTotalPay(payTime);
        Map<String, Object> payMap = userPayService.selectMonthPayList(payTime, page);
        return Result.build(ResultCode.SUCCESS, new FinanceIndexListVo(payMonth, payTime, payMap));
    }

    @Override
    public Result selectAllUser() {
        EntityWrapper<WechatUser> entityWrapper = new EntityWrapper<>();

        List<WechatUser> wechatUsers = baseMapper.selectList(entityWrapper);
        List<UserVo> userVos = new ArrayList<>();
        for (int i = 0; i < wechatUsers.size(); i++) {
            UserVo userVo = new UserVo();
            userVo.setUserId(wechatUsers.get(i).getId() + "");
            userVo.setUserName(wechatUsers.get(i).getUserName());
            userVos.add(userVo);
        }
        return Result.build(ResultCode.SUCCESS, userVos);


    }
}
